package com.hod.client.lodging;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.DatePicker;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.DatePickerListener;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
import com.gwtext.client.widgets.grid.AbstractSelectionModel;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.grid.event.RowSelectionListenerAdapter;
import com.gwtext.client.widgets.layout.FormLayout;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataObject;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.charge.Charge;
import com.hod.client.charge.ChargeCRUD;
import com.hod.client.charge.ChargeGrid;
import com.hod.client.checkout.CheckOut;
import com.hod.client.client.Client;
import com.hod.client.client.ClientCRUD;
import com.hod.client.client.ClientContactData;
import com.hod.client.client.ClientContactDataGrid;
import com.hod.client.client.ClientRecordset;
import com.hod.client.frontend.CRUD;
import com.hod.client.frontend.HODForm;
import com.hod.client.reports.CheckOutDataTableView;
import com.hod.client.reserves.Reservation;
import com.hod.client.reserves.ReservationCRUD;
import com.hod.client.reserves.ReservationGrid;
import com.hod.client.reserves.ReservationRecordset;
import com.hod.client.reserves.ReservationState;
import com.hod.client.room.Room;
import com.hod.client.room.RoomCRUD;
import com.hod.client.room.RoomRecordset;
import com.hod.client.roomtype.RoomType;
import com.hod.client.HODScreenManager;

public class LodgingCRUD extends CRUD {

    
    //public boolean fromClientGrid = false; //para determinar si el llamado es desde la grilla de cliente
	private GearsDataStore dsCharges = (GearsDataStore) GWT.create(Charge.class);
	protected ChargeGrid chargesGrid = new ChargeGrid(false,dsCharges,false);
	protected String idLodging = "";
	
	public static void createListBox(Integer idSelected,ListBox lb){
		GearsDataStore ds = (GearsDataStore) GWT.create(Lodging.class);
		try {
			//Obtengo todos los hospedajes validos
			GearsQuery q = ds.getNamedQuery(Lodging.FIND_VALID_LODGING);
			Lodging[] data = (Lodging[]) q.execute();
			lb.clear();
			if (data.length>0){
				for (Lodging lod:data){
					lb.addItem(RoomRecordset.getRoomById(lod.getIdRoom()).getNumber(),String.valueOf(lod.getIdLodging()));
					if (idSelected.equals(lod.getIdLodging())){
						lb.setSelectedIndex(lb.getItemCount()-1);
					}
				}
			}else{
				lb.addItem("No hay habitaciones ocupadas");
			}
		} catch (DatabaseException e) {			
			// TODO Auto-generated catch block
		}
		
	}
	
	
	public LodgingCRUD(){
		ds = (GearsDataStore) GWT.create(Lodging.class);
		gridPanel = new LodgingGrid(false,ds);
	}

	@Override
	protected void buildPanelSearch() {
		// TODO Auto-generated method stub
		HODScreenManager.clients.createClientPanelSearch(panel,new ComboBoxListenerAdapter() {
			public void onSelect(ComboBox comboBox, Record record, int index) {
				gridPanel.getStore().clearFilter();
				gridPanel.getStore().filter("idClient", record.getAsString("idClient"));
				gridPanel.getSelectionModel().selectFirstRow();
			}
		},
		new EventCallback(){

			@Override
			public void execute(EventObject e) {
				// TODO Auto-generated method stub
				gridPanel.getStore().clearFilter();
			}
			
		});
	}

	@Override
	protected AbstractSelectionModel createSelectionModel() {
		// TODO Auto-generated method stub
		final RowSelectionModel sm = new RowSelectionModel(true);
        sm.addListener(new RowSelectionListenerAdapter() {
        	public void onRowSelect(RowSelectionModel sm, int rowIndex, Record record) {
                formPanel.updateDataControls(record);
                
                idLodging = record.getAsString("idLodging");
                chargesGrid.refresh(idLodging );

            }
        });
        return sm;
	}

	@Override
	protected Widget formTabs() {
		// TODO Auto-generated method stub
		final TabPanel tabPanel = new TabPanel();
		Panel chargesData = new Panel(); 
		
        tabPanel.setActiveTab(0);
               
        chargesData.setTitle("Charges");
        chargesData.setLayout(new FormLayout());
        chargesData.setAutoHeight(true);
        chargesData.setPaddings(10);
        chargesData.add(chargesGrid);

        tabPanel.add(chargesData);
        
        
        
        return tabPanel;
	}


	@Override
	public HODForm createFormPanel() {
		// TODO Auto-generated method stub
		if (formPanel==null) formPanel = new LodgingFormPanel(ds);
		return formPanel;
	}


	public void setIdClient(int idClient) {
		((LodgingFormPanel)formPanel).setIdClient(idClient);
	}


	public void setRooms(Room[] roomsAvailable) {
		// TODO Auto-generated method stub
		((LodgingFormPanel)formPanel).setRooms(roomsAvailable);
		
	}


	public void setIdReservation(int idReservation) {
		// TODO Auto-generated method stub
		((LodgingFormPanel)formPanel).setIdReservation(idReservation);
		
	}
	
	

}
